﻿

var postApiUrl = '/api/WallPost/', commentApiUrl = '/api/FeedsComment/';


// Model
function Post(data) {
  
    var self = this;
    data = data || {};
    self.FeedId = data.FeedId;
    self.Imageurl = ko.observable(data.Imageurl || "");
    self.Title = data.Title || "";
    self.SourceId = data.SourceId || "";
    self.ShortDescription = data.ShortDescription || "";
    self.LinkUrl = data.LinkUrl;
    self.FbShareLink = data.FbShareLink;
    self.TwitterShareLink = data.TwitterShareLink;
    self.LikeCount = data.LikeCount;
    self.PostedDate = data.PostedDate;
    self.CommentCount = data.CommentCount;
    self.CommentedBy = data.CommentedBy;
    self.CommentedByAvatar = data.CommentedByAvatar;
    self.CommentedOn = data.CommentedOn;
    self.Description = data.Description;
    self.newCommentMessage = ko.observable();
    self.PostComments = ko.observableArray();
    self.LikeOrUnlike = data.LikeOrUnlike;
    
    self.addComment = function() {
        var comment = new Comment();
        comment.FeedId = self.FeedId;
        comment.Description(self.newCommentMessage());
        return $.ajax({
            url: commentApiUrl,
            dataType: "json",
            contentType: "application/json",
            cache: false,
            type: 'POST',
            data: ko.toJSON(comment)
        })
            .done(function(result) {
                self.PostComments.push(new Comment(result));
                self.newCommentMessage('');
            })
            .fail(function() {
                error('unable to add post');
            });


    };
    if (data.PostComments) {
        var mappedPosts = $.map(data.PostComments, function (item) { return new Comment(item); });
        self.PostComments(mappedPosts);
    }
    self.toggleComment = function(item, event) {
        $(event.target).next().find('.publishComment').toggle();
    };

}



function Comment(data) {
    var self = this;
    data = data || {};
  
    // Persisted properties
    self.CommentId = data.CommentId;
    self.FeedId = data.FeedId;
    self.userId = 0;
    self.Name = ko.observable();
    self.Description = ko.observable(data.Description || "");
    self.CommentedBy = data.CommentedBy || "";
    self.CommentedOn =  getTimeAgo(data.CommentedOn);
    self.CommentedByAvatar = data.CommentedByAvatar || "";
    self.ReplyCommentId = ko.observable();
    //persist edits to real values on accept
    self.deleteComment = function() {

    };

}

function LikeCounter(data) {
    var self = this;
    data = data || {};

    // Persisted properties
   
    self.PostId = data.PostId;
    self.Message = ko.observable(data.Message || "");
    self.CommentedBy = data.CommentedBy || "";
    self.CommentedByAvatar = data.CommentedByAvatar || "";
    self.CommentedByName = data.CommentedByName || "";
    self.CommentedDate = getTimeAgo(data.CommentedDate);
    self.error = ko.observable();
    //persist edits to real values on accept
    self.deleteComment = function () {

    };

}

function getTimeAgo(varDate) {
    if (varDate) {
        return $.timeago(varDate.toString().slice(-1) == 'Z' ? varDate : varDate+'Z');
    }
    else {
        return '';
    }
}



function PostViewModel() {
    this.Post = ko.observable();
    this.FeedSourceDetail = ko.observable();
}
var feedSourceDetail = "";
var source = "";
function viewModel() {
    var self = this;
    self.posts = ko.observableArray();
    self.newMessage = ko.observable();
    self.error = ko.observable();
    self.source = ko.observable();
   
    self.Source = ko.observable();
    self.loadComments = function (feedtype, CategoryId) {
        //To load existing posts
        $.blockUI({
            message: 'Loading Feeds..',
            css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#fff',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                color: '#000'
            }
        });
        $.ajax({
            url: commentApiUrl,
            dataType: "json",
            data: { feedtype: feedtype, CategoryId: CategoryId },
            contentType: "application/json",
            cache: false,
            type: 'GET'
        })
     .done(function (data) {
         var mappedPosts = $.map(data, function (item) { return new Post(item); });
         self.posts(mappedPosts);
         clearJumpTags();
     })
     .fail(function () {
         error('unable to load posts');
     });
    };

    self.addComment = function () {
        var comment = new Comment();
        comment.FeedId = self.FeedId;
        comment.Description(self.newCommentMessage());
        return $.ajax({
            url: commentApiUrl,
            dataType: "json",
            contentType: "application/json",
            cache: false,
            type: 'POST',
            data: ko.toJSON(comment)
        })
            .done(function (result) {
                self.PostComments.push(new Comment(result));
                self.newCommentMessage('');
              
            })
            .fail(function () {
                error('unable to add post');
            });


    };

    self.addLike = function(data) {
        var like = new LikeData();
    };
   
    self.loadComments(feedtype, CategoryId);
    return self;
   
};

//custom bindings

//textarea autosize
ko.bindingHandlers.jqAutoresize = {
    init: function (element, valueAccessor, aBA, vm) {
        if (!$(element).hasClass('msgTextArea')) {
            $(element).css('height', '1em');
        }
        $(element).autosize();
    }
};

ko.applyBindings(new viewModel());  

